package cn.ztydata.teachingsystem.heaven.service;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.common.WebContext;
import cn.ztydata.teachingsystem.heaven.cons.Roles;
import cn.ztydata.teachingsystem.heaven.dao.CourseDao;
import cn.ztydata.teachingsystem.heaven.dao.CourseGroupDao;
import cn.ztydata.teachingsystem.heaven.dao.SpecialtyDao;
import cn.ztydata.teachingsystem.heaven.dao.TeacherTeamDao;
import cn.ztydata.teachingsystem.heaven.entity.Specialty;
import cn.ztydata.teachingsystem.heaven.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 专业群组业务逻辑类
 *
 * @author lyj
 * @since 2015/5/6
 */
@Service
public class SpecialtyGroupService extends BaseService {
    @Autowired
    private CourseGroupDao courseGroupDao;
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private SpecialtyDao specialtyDao;
    @Autowired
    private WebContext webContext;
    @Autowired
    private TeacherTeamDao teacherTeamDao;

    /**
     * 获取专业群组列表
     *
     * @param pageNum  当前页码
     * @param pageSize 每页条数
     * @return page
     * @author lyj
     * @since 2015-5-6
     */
    public Page<Specialty> getAllSpecialty(final int pageNum, final int pageSize, final User userInfo) {

        if (webContext.getRole() != Roles.SPEC_LEADER && webContext.getRole() != Roles.TEACHER) {

            log.debug("权限错误:webContext.getRole() != Roles.SPEC_LEADER && webContext.getRole() != Roles.TEACHER");

            log.warn("登录用户没有权限使用该接口！登录用户角色->{}", webContext.getRole());
            throw new ServiceException("权限错误！");
        }

        //根据专业ID获取专业群组列表
        if (getSpecialtyIds(userInfo).size() != 0) {
            log.debug("根据专业ID获取专业群组列表speList.size() != 0");

            return specialtyDao.getSpecialtiesByIds(getSpecialtyIds(userInfo), pageNum, pageSize);
        } else {
            log.debug("speList.size() == 0");
            return new Page<>();
        }
    }

    /**
     * 获取与登录用户有关系的专业ID(没有专业负责人的过滤掉)
     *
     * @param userInfo
     * @return List<Long>
     * @author lyj
     * @since 2015-5-13
     */
    @Cacheable(value = "teachingSystem", key = "'user:specialtyIds'")
    public List<Long> getSpecialtyIds(final User userInfo) {
        //存储专业ID
        Set<Long> speList = new HashSet<>();

        //根据登录老师负责的课程群组获取专业ID（只获取当前课程群组所属的专业，没有获取历史课程群组所属的专业）
        List<String> courseCodes = courseGroupDao.getCodesByTeacher(userInfo.getNumber()); //老师负责的课程群组所属的课程代码
        if (courseCodes.size() != 0) {
            log.debug("courseCodes.size() != 0");

            speList.addAll(courseDao.getSpecialtiesByCodes(courseCodes));
        }

        //获取登录老师负责的专业ID集合
        speList.addAll(specialtyDao.getIdsForLeader(webContext.getUserId()));

        //获取登录老师维护的课程所属的专业ID集合
        speList.addAll(teacherTeamDao.getSpecialtiesByTeacherId(webContext.getUserId()));

        //判断是否有专业负责人，没有专业负责人的过滤掉
        return specialtyDao.getSpecialtyIds(speList);
    }


    /**
     * 获取专业群组明细头部
     *
     * @param specId 专业ID
     * @return Specialty
     * @author lyj
     * @since 2015-5-6
     */
    public Specialty getSpecialtyTop(final long specId, final User userInfo) {

        if (webContext.getRole() != Roles.SPEC_LEADER && webContext.getRole() != Roles.TEACHER) {

            log.debug("登录用户没有权限使用该接口webContext.getRole() != Roles.SPEC_LEADER && webContext.getRole() != Roles.TEACHER");

            log.warn("登录用户没有权限使用该接口！登录用户角色->{}", webContext.getRole());
            throw new ServiceException("权限错误！");
        }

        //判断登录用户是否可以获取该专业群组信息
        if (!getSpecialtyIds(userInfo).contains(specId)) {
            throw new ServiceException("权限错误！");
        }

        log.debug("获取专业群组明细头部");
        return specialtyDao.getSpecialtyTop(specId);
    }
}
